package com.onlineshop.highgee.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.onlineshop.highgee.common.BasicDao;
import com.onlineshop.highgee.common.JdbcUtil;
import com.onlineshop.highgee.dao.UserDao;
import com.onlineshop.highgee.exception.UserDaoException;
import com.onlineshop.highgee.entity.User;
/**
 *
 * @ClassName:UserDaoImpl
 *
 * @Description:TODO 锟矫伙拷锟斤拷息实锟斤拷锟洁，锟斤拷锟� 锟斤拷锟斤拷 删锟斤拷 锟斤拷询锟斤拷锟斤拷 锟斤拷询name id
 *
 * @author:20152 LuLong
 *
 * @date:2019锟斤拷12锟斤拷20锟斤拷
 *
 */

public class UserDaoImpl implements UserDao {

	@Override
	public void addUser(User user) throws UserDaoException {
		try {
			String sql = "insert into user values(?,?,?)";
			Connection conn = JdbcUtil.getConnection();
			BasicDao.updateData(sql, conn, user.getUserId(), user.getUserName(), user.getPassword());
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Connection conn = null;
		// PreparedStatement pst = null;
		// try {
		// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
		// Class.forName("com.mysql.jdbc.Driver");
		// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
		// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
		// "root", "root");
		// // String sql = "insert into highgee_user values(?,?)";
		// String sql = "insert into highgee_user values(?,?,?)";
		// pst = conn.prepareStatement(sql);
		// pst.setInt(1, user.getUserId());
		// pst.setString(2, user.getUserName());
		// pst.setString(3, user.getPassword());
		// pst.executeUpdate();
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (pst != null)
		// pst.close();
		// if (conn != null)
		// conn.close();
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		// }
	}

	@Override
	public void updateUser(User user) throws UserDaoException {
		Connection conn = null;
		try {
			String sql = "update user set user_name=?,password=? where user_id=?";
			conn = JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			BasicDao.updateData(sql, conn, user.getUserName(), user.getPassword(), user.getUserId());
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
		}
		// PreparedStatement pst = null;
		// try {
		// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
		// Class.forName("com.mysql.jdbc.Driver");
		// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
		// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
		// "root", "root");
		// String sql = "update highgee_user set user_name=?,password=? where
		// user_id=?";
		// // String sql = "update highgee_user set user_name=? where user_id=?";
		// pst = conn.prepareStatement(sql);
		// pst.setString(1, user.getUserName());
		// pst.setString(2, user.getPassword());
		// pst.setInt(3, user.getUserId());
		// pst.executeUpdate();
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (pst != null)
		// pst.close();
		// if (conn != null)
		// conn.close();
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		// }
	}

	@Override
	public void deleteUser(User user) throws UserDaoException {
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			String sql = "delete from user where user_id=?";
			conn = JdbcUtil.getConnection();
			pst = conn.prepareStatement(sql);

			pst.setInt(1, user.getUserId());
			pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}

		// try {
		// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
		// Class.forName("com.mysql.jdbc.Driver");
		// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
		// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
		// "root", "root");
		// String sql = "delete from highgee_user where user_id=?";
		// pst = conn.prepareStatement(sql);
		// pst.setInt(1, user.getUserId());
		// pst.executeUpdate();
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (pst != null)
		// pst.close();
		// if (conn != null)
		// conn.close();
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		// }
	}

	@Override
	public List<User> findAll() throws UserDaoException {
		List<User> userList = new ArrayList<User>();
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		User user = null;
		try {
			String sql = "select * from user";
			conn = JdbcUtil.getConnection();
			pst = conn.prepareStatement(sql);
			// rs = pst.executeQuery();
			rs = pst.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(rs.getString("user_name"));
				user.setPassword(rs.getString("password"));
				userList.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null)
					pst.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		// try {
		// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
		// Class.forName("com.mysql.jdbc.Driver");
		// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
		// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
		// "root", "root");
		// String sql = "select * from highgee_user";
		// pst = conn.prepareStatement(sql);
		// // rs = pst.executeQuery();
		// rs = pst.executeQuery();
		// while (rs.next()) {
		// user = new User();
		// user.setUserId(rs.getInt("user_id"));
		// user.setUserName(rs.getString("user_name"));
		// user.setPassword(rs.getString("password"));
		// userList.add(user);
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (pst != null)
		// pst.close();
		// if (conn != null)
		// conn.close();
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		// }
		return userList;
	}

	@Override
	public User findByNameAndPassword(String name, String password) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		User user = new User();
		try {
			String sql = "select * from user where user_name=? and password=?";
			conn = JdbcUtil.getConnection();
			pst = conn.prepareStatement(sql);
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, password);
			rs = pst.executeQuery();

			while (rs.next()) {
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(rs.getString("user_name"));
				user.setPassword(rs.getString("password"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null)
					pst.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}
	// try {
	// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
	// Class.forName("com.mysql.jdbc.Driver");
	// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
	// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
	// "root", "root");
	// String sql = "select * from highgee_user where user_name=? and password=?";
	// pst = conn.prepareStatement(sql);
	// pst.setString(1, name);
	// pst.setString(2, password);
	// rs = pst.executeQuery();
	// while (rs.next()) {
	// user.setUserId(rs.getInt("user_id"));
	// user.setUserName(rs.getString("user_name"));
	// user.setPassword(rs.getString("password"));
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// } finally {
	// try {
	// if (pst != null)
	// pst.close();
	// if (conn != null)
	// conn.close();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// }
	// return user;
	// }

}
